package com.conan.zone.framework.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.conan.zone.framework.dao.IAbstractDao;
import com.conan.zone.framework.exception.DaoException;
import com.conan.zone.framework.model.ParamBean;

/**
 * 
 * @author jpeng
 *
 */
public class AbstractDaoImpl<T extends ParamBean> implements IAbstractDao<T> {
	protected String namespace;
	@Resource
	@Qualifier("mainSqlMap")
	protected SqlMapClientTemplate mainSqlMap;

	@Override
	public void init() {
		this.setNamespace("");
	}

	@SuppressWarnings("unchecked")
	@Override
	public T selectByPrimaryKey(Long pk) throws DaoException {
		return (T)mainSqlMap.queryForObject(namespace+"selectByPrimaryKey",pk);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> selectByCondition(T t, int rowStart, int rowEnd)
			throws DaoException {
		return mainSqlMap.queryForList(namespace+"select",t,rowStart,rowEnd - rowStart);
	}

	@Override
	public Integer getTotalByCondition(T t) throws DaoException {
		Integer it = (Integer)mainSqlMap.queryForObject(namespace+"count", t);
		if(it == null) return 0;
		return it;
	}

	@Override
	public Integer insertObj(T t) throws DaoException {
		Object obj = mainSqlMap.insert(namespace+"insert", t);
		if(obj!=null) return Integer.valueOf(obj.toString());
		return null;
	}

	@Override
	public Integer updateByPrimaryKey(T t) throws DaoException {
		return mainSqlMap.update(namespace+"updateByPrimaryKey",t);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> selectAll(T t) throws DaoException {
		return mainSqlMap.queryForList(namespace+"select");
	}

	@Override
	public Integer deleteByPrimaryKey(Long pk) throws DaoException {
		return mainSqlMap.delete(namespace+"deleteByPrimaryKey", pk);
	}

	
	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}
}

